﻿using System;
using System.Linq.Expressions;

namespace Soul.Expressions
{
    internal static class LambdaConvert
    {
        public static void Exchange(ref Expression left, ref Expression right)
        {
            if (left.Type == right.Type)
            {
                return;
            }
            if (GetTypeNumber(left.Type) > GetTypeNumber(right.Type))
            {
                right = Expression.Convert(right, left.Type);
            }else
            {
                left = Expression.Convert(left, right.Type);
            }
        }

        public static int GetTypeNumber(Type type)
        {
            if (type == typeof(decimal) || type == typeof(decimal?))
            {
                return 10;
            }
            if (type == typeof(double) || type == typeof(double?))
            {
                return 9;
            }
            if (type == typeof(float) || type == typeof(float?))
            {
                return 8;
            }
            if (type == typeof(long) || type == typeof(long?))
            {
                return 7;
            }
            if (type == typeof(int) || type == typeof(int?))
            {
                return 6;
            }
            if (type == typeof(short) || type == typeof(short?))
            {
                return 5;
            }
            return -1;
        }
    }
}
